#!/bin/sh

############################################################################
#                                                                          #
#              cleanit: Cleans the package temp directories                #
#                                                                          #
############################################################################

#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#  Copyright (c) 2011 Sercan Arslan <arslanserc@gmail.com>
#

. /etc/init.d/tc-functions

TET_ROOT="$(dirname $0)/.."

if [ -f "$TET_ROOT/share/tet/functions.sh" ]; then
     . "$TET_ROOT/share/tet/functions.sh"
else
     echo "${RED}$(basename $0):${BLUE} tc-ext-tools functions file not found!${NORMAL}"
     exit 1
fi

this_main() {

   echo "---------------------------------------------------------------"
   echo ""
   echo "Cleaning the package $PACKAGE ..."
   echo ""
   echo "---------------------------------------------------------------"

   echo "Removing the package bin directory"
   [ -d "$PACKAGE_BIN" ] && sudo rm -rf "$PACKAGE_BIN"
   echo "Removing the package src temporary directory"
   [ -d "$PACKAGE_SRC_TEMP" ] && sudo rm -rf "$PACKAGE_SRC_TEMP"
   echo "Removing the package pkg temporary directory"
   [ -d "$PACKAGE_PKG_TEMP" ] && sudo rm -rf "$PACKAGE_PKG_TEMP"

   echo "Removing the package pkg stamps"
   if [ -e "$PACKAGE_CREATE_STAMP" ]; then
        sudo rm -f "$PACKAGE_PKG"/*.stamp
        touch "$PACKAGE_CREATE_STAMP"
   else
        sudo rm -f "$PACKAGE_PKG"/*.stamp
   fi

   if [ -f "$PACKAGE_SOURCE_CONV" -a -f "$PACKAGE_SOURCE_ORIG" ] && [ "$PACKAGE_SOURCE_CONV" != "$PACKAGE_SOURCE_ORIG" ]; then
        echo "Removing the package source original"
        sudo rm -f "$PACKAGE_SOURCE_ORIG"
        sudo rm -f "$PACKAGE_DOWNLOAD_STAMP"
   fi

   echo "---------------------------------------------------------------"
   echo ""
   echo "Cleaned the package $PACKAGE successfully."
   echo ""
   echo "---------------------------------------------------------------"

   return 0

}

echo -e "${MESSAGE_COLOR}Cleaning the package ${MESSAGE_COLOR_H}$PACKAGE${MESSAGE_COLOR} files and directories ... ${NORMAL}\c"
this_main > "$PACKAGE_MESSAGE_LOG" 2>&1
if [ "$?" -gt 0 ]; then
     echo "${ERROR_COLOR}failed!${NORMAL}"
     echo "${WARNING_COLOR}For details: ${WARNING_COLOR_H}buildit --print message${NORMAL}"
     exit 1
fi
echo "${MESSAGE_COLOR_H}successful!${NORMAL}"

exit 0

